//
//  ICResetPasswardController.m
//  ICome
//
//  Created by ENN on 16/3/30.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICResetPasswardViewController.h"
#import "ZYKeyboardUtil.h"
#import "ICCodeView.h"
#import "IComMediator.h"
#import "ICLoginNetworkManager.h"
#import "ICHeaderFile.h"

@interface ICResetPasswardViewController()<UITextFieldDelegate>

@property (strong, nonatomic)ZYKeyboardUtil *keyboardUtil;
@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UITextField *safetyCodeTextField;
@property (nonatomic, strong)UITextField *passwardTextField;
@property (nonatomic, strong)ICCodeView *codeView;
@property (nonatomic, strong)UIButton *finishButton;
@property (nonatomic, strong)UIView *bottomView;
@property (nonatomic, strong)UIView *bottomView_1;

@end
@implementation ICResetPasswardViewController

#pragma mark - life cycle
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    [self setNav];
    
    WEAKSELF
    self.keyboardUtil = [[ZYKeyboardUtil alloc] init];
    [self.keyboardUtil setAnimateWhenKeyboardAppearAutomaticAnimBlock:^(ZYKeyboardUtil *keyboardUtil) {
        [keyboardUtil adaptiveViewHandleWithController:weakSelf adaptiveView:weakSelf.safetyCodeTextField,weakSelf.passwardTextField, nil];
    }];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf.view endEditing:YES];
    }];
    [self.view addGestureRecognizer:tap];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextChanged) name:UITextFieldTextDidChangeNotification object:self.safetyCodeTextField];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextChanged) name:UITextFieldTextDidChangeNotification object:self.passwardTextField];
}

- (void)textFieldTextChanged {
    self.finishButton.enabled = (_safetyCodeTextField.text.length>0 && _passwardTextField.text.length == 4);
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self.codeView changeCode];
}

#pragma mark - CustomDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.view endEditing:YES];
    return YES;
}

#pragma mark - event response
- (void)finishButtonAction {
    if (![ICTools stringEmpty:self.safetyCodeTextField.text]) {
        if ([self.passwardTextField.text isEqualToString:self.codeView.changeString]) {
            [MBProgressHUD showLoadingActivityView:self.view];
            [[ICLoginNetworkManager sharedInstance] getMobile:self.safetyCodeTextField.text success:^(id object) {
                NSDictionary *data = [object objectForKey:@"data"];
                if (![ICTools isObjEmpty:data]) {
                    NSDictionary *dict = @{@"mobile":data[@"mobile"],
                                           @"eId":data[@"eId"]};
                    UIViewController  *viewController = [[IComMediator sharedInstance] IComMediator_EditPasswardViewController:dict];
                    [self.navigationController pushViewController:viewController animated:YES];
                }
                [MBProgressHUD hideLoadingActivityView:self.view];
            } failure:^(NSError *error) {
                [MBProgressHUD showError:@"未能获取您的信息"];
                [MBProgressHUD hideLoadingActivityView:self.view];
            }];
        } else {
            [MBProgressHUD showError:@"请输入正确的验证码"];
        }
    } else {
        [MBProgressHUD showError:@"请输入账号或者手机号"];
    }
}

#pragma mark - private methods
- (void)setUI {
    CGFloat leftMargin = 15;
    CGFloat rightMargin= 15;
    [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.bottom.equalTo(self.view.mas_bottom);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
    }];
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scrollView.mas_top).offset(40);
        make.centerX.equalTo(self.scrollView.mas_centerX);
    }];
    
    
    [self.safetyCodeTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(50);
        make.left.equalTo(self.scrollView.mas_left).offset(leftMargin);
        make.width.mas_equalTo(App_Frame_Width / 3 * 2);
    }];
    
    
    [self.bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.safetyCodeTextField.mas_bottom).offset(12);
        make.left.equalTo(self.view.mas_left).offset(leftMargin);
        make.right.equalTo(self.view.mas_right).offset(-rightMargin);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.passwardTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.safetyCodeTextField.mas_bottom).offset(30);
        make.left.equalTo(self.scrollView.mas_left).offset(leftMargin);
        make.width.mas_equalTo(App_Frame_Width / 3 * 2);
    }];
    
    self.codeView.backgroundColor = [UIColor whiteColor];
    
    [self.bottomView_1 mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwardTextField.mas_bottom).offset(12);
        make.left.equalTo(self.view.mas_left).offset(leftMargin);
        make.right.equalTo(self.view.mas_right).offset(-rightMargin);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.finishButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwardTextField.mas_bottom).offset(50);
        make.centerX.equalTo(self.scrollView.mas_centerX);
        make.left.equalTo(self.scrollView.mas_left).offset(leftMargin);
        make.width.mas_equalTo(App_Frame_Width - leftMargin * 2);
    }];
}

// 设置navigation
- (void)setNav {
    self.title = @"忘记密码";
}
// 网络请求
- (void)getData {
    // 发送新密码
}

#pragma mark - getters and setters
-(UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.autoresizingMask = 0xFF;
        _scrollView.contentMode = UIViewContentModeCenter;
        _scrollView.contentSize = CGSizeMake(3 * CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.scrollView.frame));
        self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.scrollView.frame), 0);
        self.scrollView.pagingEnabled = YES;
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}

-(UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = ICFont(Level * 20);
        _titleLabel.textColor = ICTEXTCOLOR_T1;
        _titleLabel.text = @"请输入账号或手机号";
        [self.scrollView addSubview:_titleLabel];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

-(UITextField *)safetyCodeTextField {
    if (!_safetyCodeTextField) {
        _safetyCodeTextField = [[UITextField alloc] init];
        _safetyCodeTextField.font = [UIFont systemFontOfSize:16];
        _safetyCodeTextField.textColor = ICTEXTCOLOR_T1;
        _safetyCodeTextField.textAlignment = NSTextAlignmentLeft;
        [self.scrollView addSubview:_safetyCodeTextField];
        _safetyCodeTextField.placeholder = @"请输入账号或手机号";
        _safetyCodeTextField.keyboardType = UIKeyboardTypeASCIICapable;
        _safetyCodeTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入账号或手机号" attributes:@{NSForegroundColorAttributeName:ICRGB(0x676767),NSFontAttributeName:[UIFont systemFontOfSize:14.0]}];
        
        _safetyCodeTextField.delegate = self;
    }
    return _safetyCodeTextField;
}

-(ICCodeView *)codeView {
    if (!_codeView) {
        _codeView = [[ICCodeView alloc] initWithFrame:CGRectMake(App_Frame_Width - 80 - 15, 153, 80, 40)];
        [self.scrollView addSubview:_codeView];
        WEAKSELF;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            [weakSelf.codeView changeCode];
        }];
        [_codeView addGestureRecognizer:tap];
    }
    return _codeView;
}

-(UITextField *)passwardTextField {
    if (!_passwardTextField) {
        _passwardTextField = [[UITextField alloc] init];
        _passwardTextField.font = [UIFont systemFontOfSize:16];
        _passwardTextField.textColor = ICTEXTCOLOR_T1;
        _passwardTextField.textAlignment = NSTextAlignmentLeft;
        _passwardTextField.keyboardType = UIKeyboardTypePhonePad;
        _passwardTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:@{NSForegroundColorAttributeName:ICRGB(0x676767),NSFontAttributeName:[UIFont systemFontOfSize:14.0]}];
        _passwardTextField.delegate = self;
        [self.scrollView addSubview:_passwardTextField];
    }
    return _passwardTextField;
}

-(UIButton *)finishButton {
    if (!_finishButton) {
        _finishButton = [[UIButton alloc] init];
        [_finishButton setTitle:@"下一步" forState:UIControlStateNormal];
        [_finishButton addTarget:self action:@selector(finishButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [_finishButton setBackgroundImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_detail_sender"] forState:UIControlStateNormal];
        [self.scrollView addSubview:_finishButton];
        _finishButton.enabled = NO;
    }
    return _finishButton;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        [self.scrollView addSubview:_bottomView];
        _bottomView.backgroundColor = ICRGB(0x9095a5);
    }
    return _bottomView;
}

-(UIView *)bottomView_1 {
    if (!_bottomView_1) {
        _bottomView_1 = [[UIView alloc] init];
        [self.scrollView addSubview:_bottomView_1];
        _bottomView_1.backgroundColor = ICRGB(0x9095a5);
    }
    return _bottomView_1;
}
-(void)dealloc {
}


@end
